package com.neusoft.hifly.core.token.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.hifly.core.token.JwtUtil;

/**
 * 设定返回头信息，解决跨域问题
 */
@Component
public class HttpInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler)
			throws Exception {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		// 允许自定义请求头token(允许head跨域)
		response.setHeader("Access-Control-Allow-Headers",
				JwtUtil.TOKEN_KEY + ", Accept, Origin, X-Requested-With, Content-Type, Last-Modified");
		return true;
	}

	@Override
	public void afterCompletion(final HttpServletRequest arg0, final HttpServletResponse arg1, final Object arg2,
			final Exception arg3) throws Exception {

	}

	@Override
	public void postHandle(final HttpServletRequest arg0, final HttpServletResponse arg1, final Object arg2,
			final ModelAndView arg3) throws Exception {

	}

}
